Time Retrieval
Volume Number: 5
Issue Number: 1
Column Tag: Basic School
Related Info: Time Manager
Time Retrieval and Storage Techniques
By Dave Kelly, MacTutor Editorial Board
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Time Retrieval and Storage Techniques
It’s only a matter of time. Anybody see the ad for the MacTutor of the future in
the November, 1988 MacTutor? The ad may sound like a joke about the 100th
anniversary of MacTutor, but some of the “future” has already been invented. I did
some thought on the subject of the Basic School column described in the ad, which is
also the title of this column. There are two basic subjects implied by the title, time
retrieval and time storage.
From a philosophical point of view you may ask, how can time be saved or
retrieved? Looking at the answer from that perspective would lead us to a discussion
of Einstein and his theory of relativity which is beyond the scope and intent of this
column. Instead let’s look at some ways which we can use time to our advantage and see
how storage and retrieval fits into the scheme of things.
From a user level on the Macintosh there is one time retrieval/storage
mechanism that is accessible to all “the rest of us”. The example I refer to is the
Alarm mechanism built into the clock chip. The date and time setting is copied
(“retrieved”) at system startup from the clock chip into its own low-memory
location. It’s stored there as the number of seconds since midnight, January 1, 1904,
and is updated every second. The range of the clock chip is from January 1, 1904
through February 6, 2040 (does that mean that 51 years from now I’ll need to replace
my clock chip?). The low-memory location can be access by your program. BASIC
(every brand) includes its own time retrieval command. In True Basic the TIME and
TIME$ functions are provided. TIME returns the number of seconds since midnight in
tenths of seconds. The TIME$ function returns a string that contains the time as
measured by the 24-hour clock with resolution in seconds. ZBasic and MS QuickBASIC
uses the TIMER function which returns the seconds since midnight (similar to the True
Basic TIME function) and TIME$ which works the same as in True Basic. The problem
is that the TIMER function does not provide enough resolution. At least the True Basic
TIME function provided tenths of seconds or better resolution. Since we know that
other languages are able to access the time in greater resolution, we may use the same
function (ROM) to retrieve the time in greater resolution. This is e specially
important for benchmark tests where two compiled programs are being timed and the
difference is tenths of seconds or better. In benchmarks that have been done in the
past, sometimes the TIME function was used and there was rounding off to either 0 or 1
second. A program running in 0 seconds is absurd, but that was the best we could do
with MS Basic at the time. QuickBASIC and ZBasic both allow access to the TickCount
call (Macintosh ROM). In QuickBASIC a typical benchmark might be implemented such
as:
Figure 1. The Time Machine
First initialize the ToolBox routine:
TOOLBOX “I”
This must be used before any other ToolBox statement in the program. Now
define a sub program or routine that can be called to replace the other time functions
usually used in the benchmark.
SUB Tick (Count&) STATIC
TrapNo% = &HA975
Count& = 0& : ‘Define the variable
ToolBox “L”, TrapNo%, Count&
END SUB
Now call Tick(Count&) whenever you need the time. A tick is one-sixtieth of a
second, enough for the tenth of a second accuracy provided by True BASIC’s function.
In ZBasic it is even easier to call TickCount. In ZBasic just use the function directly:
Count&=FN TICKCOUNT
As you can see, accessing real time is really the simple part of the job. Accessing
stored time is another aspect. I’ll leave the subject of how the stored time became
stored for later except to say that the Parameter RAM also stores the alarm setting.
You can set the alarm with the Alarm Clock DA (included with your system software).
The Analog Alarm Clock program included here demonstrates how you can